using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using NAnt.Core;
using NAnt.Core.Attributes;
using WizardControls;

namespace Deployment.CustomTasks.Types
{
	[ElementName("screen")]
	public class WizardScreenElement : Element
	{
		private WizardScreenItemElement[] itemElements;
		private Dictionary<string, WizardScreenItemElement> itemDictionary;
		private string title;

		[TaskAttribute("title", Required = true)]
		public string Title
		{
			get { return title; }
			set { title = value; }
		}

		private string wizardName;

		public string WizardName
		{
			get { return wizardName; }
			set { wizardName = value; }
		}

		public Form GetForm()
		{
			
			WizardScreenControl control = new WizardScreenControl();
			Point location = new Point(0,0);
			foreach (WizardScreenItemElement element in itemElements)
			{
				control.AddUserControl(element);
			}
			control.WizardTitle = WizardName;
			control.Title = Title;

			IWizardScreenControl iWizardScreenControl = control as IWizardScreenControl;
			iWizardScreenControl.Cancel += OnCancel;
			
			return control;
		}

		private void OnCancel(object sender, EventArgs e)
		{
			cancel = true;
		}

		[BuildElementArray("item", ElementType = typeof(WizardScreenItemElement))]
		public WizardScreenItemElement[] ItemElements
		{
			get { return itemElements; }
			set { itemElements = value; }
		}

		public virtual void ExecuteTask()
		{
			MessageBox.Show("Wizard Screen: " + GetType().FullName);
		}

		private bool cancel = false;

		public bool Cancel
		{
			get { return cancel; }
		}

		public void Show()
		{
			WizardScreenControl control = new WizardScreenControl();
			itemDictionary = new Dictionary<string, WizardScreenItemElement>();

			WizardScreenItemElement previous = null;
			Array.ForEach(itemElements, 
			              delegate(WizardScreenItemElement item)
			              	{
//			              		if (null != previous)
//			              			item.Previous = previous;
			              		itemDictionary.Add(item.Id, item);
			              		if (!string.IsNullOrEmpty(item.Depends))
			              			itemDictionary[item.Depends].DepencencyChanged += item.OnDependencyChanged;
						
			              	});


		}
	}
}